package edu.towson.cosc.cosc455.echapp1.markdowncompiler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Compiler {
	/**
	 * Checks for a valid argument. It then calls for the syntaxer
	 * The reason for this is because calling the lexer results in multiple
	 * Instances, which can be problematic. After the syntax
	 * analyzer finishes, it then calls the semantic analyzer
	 * for static scoping. Finally, it calls the portion of the semantic
	 * analyzer which writes the output file, passing it the filename to use.
	 */

	static String token;
	static String filetext;
	static String filename;
	private static MySyntaxAnalyzer syntaxer = new MySyntaxAnalyzer();
	private static MySemanticAnalyzer analyzer = new MySemanticAnalyzer();
	static ArrayList<String> StandardList = new ArrayList<String>();
	static ArrayList<String> BackupList = new ArrayList<String>();
	public static void main(String[] args) throws CompilerException, IOException {
		token = "";
		
		
		if(args.length !=1){
			String message = "Please input only a valid markdown file!";
			throw new CompilerException(message);
		}
		//2. Check for .mkd extension --> endswith
		if(!args[0].endsWith(".mkd")){
			String message = "This is not a valid markdown file!";
			throw new CompilerException(message);
		}
		//3. inits
		filename = args[0].substring(0, args[0].length() - 4);
		filetext = new Scanner(new File(args[0])).useDelimiter("\\A").next();
		//5. Call document()
		syntaxer.document();
		
		//6. Call semantic analyzer
		analyzer.SemanticAnalyzer();
		analyzer.WritetoFile(filename);
	}

}
